package service

import (
	"net/http"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/tycme/gin-chat/models"
)

func ChatList(c *gin.Context) {
	RoomId := c.Query("room_id")
	if RoomId == "" {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "房间名不能为空",
		})
		return
	}
	// 判断用户是否属于该房间
	//uc := c.MustGet("user_claims").(*helper.UserClaims)
	//_, err := models.GetUserRoomByUserIdRoomId(uc.Identity, RoomId)
	//if err != nil {
	//	c.JSON(http.StatusOK, gin.H{
	//		"code": -1,
	//		"msg":  "非法访问",
	//	})
	//	return
	//}
	pageIndex, _ := strconv.ParseInt(c.Query("page_index"), 10, 32)
	pageSize, _ := strconv.ParseInt(c.Query("page_size"), 10, 32)

	created_at, err := strconv.ParseInt(c.Query("created_at"), 10, 32)
	if err != nil {
		created_at = time.Now().Unix()
		//fmt.Printf("%T, %v\n", s, s)
	}
	skip := (pageIndex - 1) * pageSize
	data, err := models.GetMessageListByRoomId(RoomId, created_at, &pageSize, &skip)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "系统异常" + err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "数据加载成功",
		"data": gin.H{
			"list": data,
		},
	})
}
